/*-----------------------------------------------------------------------------------
Name: Sangyoon Park
Date: July 23 2020
This do file does : 
	Create variable labels and export codebook
-----------------------------------------------------------------------------------*/
clear all
set matsize 10000
set maxvar 10000
set more off


#delim ;


*** Load dataset;

use "${data_path}/INTERMEDIARY_COMBINETRADE_CLEAN", replace;


*** Average by followup survey round;

** Sum volume by buyer type;

forvalues i = 1(1)4 {;

	bys traderid followup: egen volume_type`i' = sum(sell_volume) if sell_type == `i';
	replace volume_type`i' = 0 if volume_type`i' == .;
	};


** Sum volume by market;
bys traderid followup: egen volume_china = sum(sell_volume) if sell_market == 1;
replace volume_china = 0 if volume_china == .;
bys traderid followup: egen volume_asia = sum(sell_volume) if sell_market == 2;
replace volume_asia = 0 if volume_asia == .;
bys traderid followup: egen volume_eu = sum(sell_volume) if sell_market == 3;
replace volume_eu = 0 if volume_eu == .;
bys traderid followup: egen volume_dom = sum(sell_volume) if sell_market == 4;
replace volume_dom = 0 if volume_dom == .;


** Average price by market;
gen weight_volume = sell_volume/volume_china if sell_market == 1;
replace weight_volume = sell_volume/volume_asia if sell_market == 2;
replace weight_volume = sell_volume/volume_eu if sell_market == 3;
replace weight_volume = sell_volume/volume_dom if sell_market == 4; 

gen weight_price = sell_price*weight_volume;

forvalues i = 1(1)4 {;
	bys traderid followup: egen price_mkt`i' = sum(weight_price) if sell_market == `i';
	replace price_mkt`i' = 0 if price_mkt`i' == .;
	};
	

** Average contract by buyer type;

forvalues i = 1(1)4 {;

	bys traderid followup: egen contract_type`i' = mean(sell_contract) if sell_type == `i';
	bys traderid followup: egen exp_type`i' = mean(sell_exp) if sell_type == `i';
	};

** Collapse by id and survey round;

collapse (mean) volume_type1 volume_type2 volume_type3 volume_type4 volume_china //
volume_asia volume_eu volume_dom price_mkt1 price_mkt2 price_mkt3 price_mkt4 contract_type1 //
exp_type1 contract_type2 exp_type2 contract_type3 exp_type3 contract_type4 exp_type4 //
sell_contract sell_exp, by(traderid followup);
	
** Reshape from long to wide (for merging);

gen foll = "_fu0" if followup == 0;
replace foll = "_fu1" if followup == 1;
replace foll = "_fu2" if followup == 2;
drop followup;
reshape wide volume_type1-sell_exp, i(traderid) j(foll) string;

tempfile INTERMEDIARY_TRADE_FORMERGE;
save `INTERMEDIARY_TRADE_FORMERGE';

** Merge basic and trade data;

use "${data_path}/INTERMEDIARY_COMBINESURVEY_CLEAN";

merge 1:1 traderid using `INTERMEDIARY_TRADE_FORMERGE';
drop _merge;

save "${data_path}/INTERMEDIARY_MERGED", replace;

gen firm_type =.;
replace firm_type = 1 if volume_type2_fu0 != 0;
replace firm_type = 2 if volume_type3_fu0 != 0 | volume_type4_fu0 != 0;
replace firm_type = 3 if cooperative == 1;
tab firm_type, gen(dum_firmtype);


tempfile INTERMEDIARY_MERGE;
save `INTERMEDIARY_MERGE';

** Reshape to long;

reshape long;

local varbase "buy_avgprice sell_avgprice trade_volume totalrev cost_material cost_labor cost_utility";

foreach y of local varbase {;

	gen `y' = `y'_fu0 if foll == "_fu0";
	replace `y' = `y'_fu1 if foll == "_fu1";
	replace `y' = `y'_fu2 if foll == "_fu2";
	};

local varbase "sell_contract sell_exp volume_china volume_asia volume_eu volume_dom";

foreach y of local varbase {;
	
	bys traderid: gen `y'_fu0 = `y' if foll == "_fu0";
	bys traderid: replace `y'_fu0 = `y'_fu0[_n-1] if foll == "_fu1";
	bys traderid: replace `y'_fu0 = `y'_fu0[_n-1] if foll == "_fu2";

	};	

gen round = 1 if foll == "_fu1";
replace round = 2 if foll == "_fu2";
drop if foll == "_fu0";

*** Generate variables;
	
	summ ghp_score_fu1 if st_tgroup == 1;
	local var_mean = r(mean);
	local var_sd = r(sd);
	gen std_score_fu1 = (ghp_score_fu1 - `var_mean')/`var_sd';
	
	summ gaptest_score_fu1 if st_tgroup == 1;
	local var_mean = r(mean);
	local var_sd = r(sd);
	gen std_gaptest_fu1 = (gaptest_score_fu1 - `var_mean')/`var_sd';
	
	forvalues i = 1(1)2 {;
	
	gen implied_rev_fu`i' = trade_volume_fu`i'*(sell_avgprice_fu`i' - buy_avgprice_fu`i')*(1/1000);
	gen cost_total_fu`i' = cost_labor_fu`i' + cost_utility_fu`i' + cost_material_fu`i';
	gen profit_fu`i' = totalrev_fu`i' - cost_total_fu`i';
	gen implied_prof_fu`i' = implied_rev_fu`i' - cost_total_fu`i';
	
	gen log_buy_avgprice_fu`i' = log(buy_avgprice_fu`i'+1);
	gen log_sell_avgprice_fu`i' = log(sell_avgprice_fu`i'+1);
	gen log_trade_volume_fu`i' = log(trade_volume_fu`i' + 1);
	gen log_totalrev_fu`i' = log(totalrev_fu`i' + 1);
	gen log_implied_rev_fu`i' = log(implied_rev_fu`i' + 1);
	gen log_cost_labor_fu`i' = log(cost_labor_fu`i' + 1);
	gen log_cost_utility_fu`i' = log(cost_utility_fu`i' + 1);
	gen log_cost_material_fu`i' = log(cost_material_fu`i' + 1);
	gen log_cost_total_fu`i' = log(cost_total_fu`i' + 1);
	summ profit_fu`i';
	local min_profit = r(min);
	gen log_profit_fu`i' = log(profit_fu`i' - `min_profit' +1);
	summ implied_prof_fu`i';
	local min_profit = r(min);
	gen log_implied_prof_fu`i' = log(implied_prof_fu`i' - `min_profit' +1);
	
*** Scale units;
	replace buy_avgprice_fu`i' = buy_avgprice_fu`i'/1000;
	replace sell_avgprice_fu`i' = sell_avgprice_fu`i'/1000;	
	
	};	
	
	gen implied_rev = trade_volume*(sell_avgprice - buy_avgprice)*(1/1000);
	gen cost_total = cost_labor + cost_utility + cost_material;
	gen profit = totalrev - cost_total;
	sum profit, detail;
	local min_profit = r(min);
	gen pos_profit = profit - `min_profit';
	*replace profit = pos_profit;
	gen implied_prof = implied_rev - cost_total;
	sum implied_prof, detail;
	local min_profit = r(min);
	gen pos_implied_profit = implied_prof - `min_profit';
	*replace implied_prof = pos_imp_profit;
	
	gen log_buy_avgprice = log(buy_avgprice +1);
	gen log_sell_avgprice = log(sell_avgprice+1);
	gen log_trade_volume = log(trade_volume + 1);
	gen log_volume_china = log(volume_china+1);
	gen log_volume_asia = log(volume_asia+1);
	gen log_volume_eu = log(volume_eu+1);
	gen log_totalrev = log(totalrev + 1);
	gen log_implied_rev = log(implied_rev + 1);
	gen log_cost_labor = log(cost_labor + 1);
	gen log_cost_utility = log(cost_utility + 1);
	gen log_cost_material = log(cost_material + 1);
	gen log_cost_total = log(cost_total + 1);
	gen log_profit = log(pos_profit + 1);
	gen log_implied_prof = log(pos_implied_prof+1);
	
	forvalues i = 1(1)4 {;
		replace price_mkt`i' = . if price_mkt`i' == 0;
		gen log_price_mkt`i' = log(price_mkt`i'+1);
	};	

*** Scale units;
	replace buy_avgprice = buy_avgprice/1000;
	replace sell_avgprice = sell_avgprice/1000;	
	
save "${data_path}/INTERMEDIARY_LONG", replace;
clear;

use `INTERMEDIARY_MERGE';
	

*** Prepare baseline data to use as control;

	
*** Scale units;
	replace buy_avgprice_fu0 = buy_avgprice_fu0/1000;
	replace sell_avgprice_fu0 = sell_avgprice_fu0/1000;
	egen std_knowledge = std(gaptest_score_fu1);
	
/*-----------------------------------------------------------------------------------
			 		Label 
-----------------------------------------------------------------------------------*/
	lab var age "Years of intermediation business";
	lab var size_facility "Size of packing/collection facility $(m^2)$";
	lab var dum_firmtype1 "Type = collector";
	lab var dum_firmtype2 "Type = export enterprise";
	lab var dum_firmtype3 "Type = cooperative";
	lab var trade_volume_fu0 "Trade volume (tons)";
	lab var buy_avgprice_fu0 "Average purchase price (1,000 VND/kg)";
	lab var sell_avgprice_fu0 "Average sales price (1,000 VND/kg)";
	lab var ghp_score_fu0 "Self-reported GAP compliance";
	lab var sell_contract_fu0 "Contract with buyer";
	lab var sell_exp_fu0 "Years of experience with buyer";
	lab var volume_china_fu0 "Volume of Chinese exports (tons)";
	lab var volume_asia_fu0 "Volume of Asian exports (tons)";
	lab var volume_eu_fu0 "Volume of EU/US exports (tons)";
	lab var volume_dom_fu0 "Volume of domestic sales (tons)";
	lab var cost_labor_fu0 "Expenses on labor (1M VND)";
	lab var cost_utility_fu0 "Expenses on utility (1M VND)";
	lab var cost_material_fu0 "Expenses on materials (1M VND)";
	lab var gaptest_score_fu1 "GAP test raw score";
	
*** Keep only baseline variables for merging with farmer dataset;

	keep traderid strata st_tgroup age size_facility trade_volume_fu0 buy_avgprice_fu0 sell_avgprice_fu0 cost_labor_fu0 cost_utility_fu0 cost_material_fu0 ghp_score_fu0 export_asian export_other dum_firmtype1 dum_firmtype2 dum_firmtype3 sell_contract_fu0 sell_exp_fu0 volume_china_fu0 volume_asia_fu0 volume_eu_fu0 volume_dom_fu0 std_knowledge gaptest_score_fu1 ghp_score_fu1;
	sort strata st_tgroup;
	collapse (mean) age size_facility ghp_score_fu0 export_asian export_other trade_volume_fu0 buy_avgprice_fu0 sell_avgprice_fu0 cost_labor_fu0 cost_utility_fu0 cost_material_fu0 volume_china_fu0 volume_asia_fu0 volume_eu_fu0 volume_dom_fu0 sell_contract_fu0 sell_exp_fu0 dum_firmtype1 dum_firmtype2 dum_firmtype3 std_knowledge gaptest_score_fu1 ghp_score_fu1, by(strata st_tgroup);

*** Rename variables;

	local renamevar "age size_facility ghp_score_fu0 export_asian export_other trade_volume_fu0 buy_avgprice_fu0 sell_avgprice_fu0 cost_labor_fu0 cost_utility_fu0 cost_material_fu0 volume_china_fu0 volume_asia_fu0 volume_eu_fu0 volume_dom_fu0 sell_contract_fu0 sell_exp_fu0 dum_firmtype1 dum_firmtype2 dum_firmtype3 std_knowledge gaptest_score_fu1 ghp_score_fu1";

	foreach y of local renamevar {;
	
		rename `y' int_`y';
		
		};
	
*** Save dataset;

	save "${data_path}/INTERMEDIARY_GROUPAVG", replace;

	clear;







